<?php

class ServiceMode extends SiteMode {
    /** @return ServiceMode */
    public static function init() {
        return parent::init();
    }
    public function __construct(){
        parent::__construct();
        $this->user=UserModule::init('user');
    }
    //搜索服务站的信息 TODO 图片目前只搜了id
    function getDetail($id){
        $sql="SELECT * FROM `service_info` WHERE `id`=? and site_id=?";
        return $this->db->getOne($sql,$id, $this->siteId);
    }
    //TODO  条件搜索
    function getList($enable = 'all',$rows = 99,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        switch($enable){
            case 0:$cond=" `enable`=0 ";break;
            case 1:$cond=" `enable`=1 ";break;
            default:$cond=" 1 ";
        }
        $sql="  SELECT SQL_CALC_FOUND_ROWS `service_info`.*,`base_address`.pid 
                FROM `service_info` LEFT JOIN `base_address`
                ON `service_info`.address_id=`base_address`.id
                WHERE $cond and `service_info`.site_id=?
                LIMIT $offset,$rows";
        
        return $this->db->getList($sql, $this->siteId);
    }
    //检查该用户是否已经申请过
    function checkExist($user_id){
        $sql="SELECT 1 FROM `service_info` WHERE `user_id`=? and site_id=?";
        return $this->db->getExist($sql,$user_id, $this->siteId);
    }
    function add($user_id,$real_name, $sex = 'male', $birth, $academic, $idnum, $idnum_pic,$header_name,
                 $header_tel, $service_name,$facade_pic,$nation=null, $tel=null, $email=null,
                 $address=null, $resume=null,$computer_skill=null,$address_id=null, $address_text=null,
                 $latitude=null,$longitude=null){
        $data['user_id']=$user_id;
        $data['real_name']=$real_name;
        $data['sex']=$sex;
        $data['birth']=$birth;
        $data['nation']=$nation;
        $data['tel']=$tel;
        $data['email']=$email;
        $data['academic']=$academic;
        $data['address']=$address;
        $data['idnum']=$idnum;
        $data['idnum_pic']=$idnum_pic;
        $data['header_name']=$header_name;
        $data['header_tel']=$header_tel;
        $data['computer_skill']=$computer_skill;
        $data['service_name']=$service_name;
        $data['facade_pic']=$facade_pic;
        $data['resume']=$resume;
        $data['address_id']=$address_id;
        $data['address_text']=$address_text;
        $data['latitude']=$latitude;
        $data['longitude']=$longitude;
        $data['site_id']=$this->siteId;
        if($this->db->insert('service_info',$data) == 1){
            $id = (int)$this->db->insertId();
            $type=$this->user->getType($user_id);
            if($type == 'shop'){
                $this->user->setType($user_id,'both');
            }else{
                $this->user->setType($user_id,'service');
            }
            return $id;
        }
        return -1;
    }
    function modify($id,$real_name, $sex = 'male', $birth, $academic, $idnum, $idnum_pic,$header_name,
                    $header_tel, $service_name,$facade_pic,$nation=null, $tel=null, $email=null,
                    $address=null, $resume=null,$computer_skill=null,$address_id=null, $address_text=null,
                    $latitude=null,$longitude=null){
        $id=(int)$id;
        $data['real_name']=$real_name;
        $data['sex']=$sex;
        $data['birth']=$birth;
        $data['nation']=$nation;
        $data['tel']=$tel;
        $data['email']=$email;
        $data['academic']=$academic;
        $data['address']=$address;
        $data['idnum']=$idnum;
        $data['idnum_pic']=$idnum_pic;
        $data['header_name']=$header_name;
        $data['header_tel']=$header_tel;
        $data['computer_skill']=$computer_skill;
        $data['service_name']=$service_name;
        $data['facade_pic']=$facade_pic;
        $data['resume']=$resume;
        $data['address_id']=$address_id;
        $data['address_text']=$address_text;
        $data['latitude']=$latitude;
        $data['longitude']=$longitude;
        return $this->db->update('service_info',$id,$data) == 1;
    }
    //单纯的删除一条service_info
    function delete($id){
        return $this->db->delete('service_info',$id) == 1;
    }
    /**
     * 审核
     * @param $id
     * @param int $authPass 0|1
     * @return bool
     */
    function auth($id,$authPass = 1){
        $data['enable']=$authPass;
        return $this->db->update('service_info',$id,$data,true) == 1;
    }
    function getServiceId($user_id){
        $sql="SELECT `id` FROM `service_info` WHERE `user_id`=? and site_id=?";
        return $this->db->getValue($sql,$user_id, $this->siteId);
    }
    function getUserId($id){
        $sql="SELECT `user_id` FROM `service_info` WHERE `id`=? and site_id=?";
        return $this->db->getValue($sql,$id, $this->siteId);
    }
    function getServiceInfoBySearch($header_name,$header_tel,$service_name){
        $condition='';
        if(!empty($header_name)){
            $condition.=" AND `header_name` like ".$this->db->quote("%{$header_name}%");
        }
        if(!empty($header_tel)){
            $condition.=" AND `header_tel` like ".$this->db->quote("%{$header_tel}%");
        }
        if(!empty($service_name)){
            $condition.=" AND `service_name` like ".$this->db->quote("%{$service_name}%");
        }
        $sql="SELECT SQL_CALC_FOUND_ROWS * FROM `service_info` WHERE 1 $condition and site_id=?
              ORDER BY `create_time` DESC ";
        echo $sql;
        return $this->db->getAll($sql, $this->siteId);
    }
}